﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Pahan.McuFusion.Common;
using Pahan.McuFusion.Common.Utils;
using Pahan.McuFusion.UI.Workspaces;

namespace Pahan.McuFusion.UI.Scripting
{
    public sealed class WorkspaceUtils
    {
        public Workspace CreateWorkspace(DeviceInfo device, ProgrammerInfo programmer,
            HardwareAdapterInfo adapter, HardwareInterfaceInfo @interface)
        {
            return new Workspace(device, programmer, adapter, @interface);
        }

        public Workspace CreateWorkspace(String vendorName, String modelName, String programmerName,
            String adapterName, String interfaceName)
        {
            var programmerInfo = AssemblyLoader.GetObjects<ProgrammerInfo>().Single(p => p.Name == programmerName);
            var deviceInfo = programmerInfo.SupportedMicrocontrollers.Single(
                d => d.Vendor == vendorName && d.Model == modelName);
            var adapterInfo = AssemblyLoader.GetObjects<HardwareAdapterInfo>().Single(
                a => a.Name == adapterName && programmerInfo.Supports(a));
            var interfaceInfo = AssemblyLoader.GetObjects<HardwareInterfaceInfo>().Single(
                i => i.Name == interfaceName && adapterInfo.Supports(i));
            return new Workspace(deviceInfo, programmerInfo, adapterInfo, interfaceInfo);
        }

        public Workspace LoadWorkspace(String path)
        {
            return Workspace.Load(new FileInfo(path));
        }

        public Workspace LoadWorkspace(FileInfo file)
        {
            return Workspace.Load(file);
        }
    }
}
